1841C - Ranom Numbers - CodeForces Solution


brute force dp greedy strings

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <unordered_map>
#define int long long
#define f(i,a,b) for(int i=a;i<b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back

using namespace std;
int md = 1e9+7 ;

int power(int x, int y){
	if(y==0){
		return 1; 
	}
	
	if(y==1){
		return x ;
	}
	int ans = power(x,y/2) ;
	ans*=ans;
	ans%=md ;
	
	if(y%2==1){
		ans*=x; 
		ans%=md ;
	}
	return ans ;
}


int32_t main(){
 
 
 ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
 
//	freopen("input.txt","r",stdin) ;
//	freopen("output.txt","w",stdout) ;

 int tt =1 ;
 cin>>tt ;



for(int ii=0 ; ii<tt ; ii++ ){
	int n;
	string s ;
	cin>>s ;
		n = s.length() ;
		
		if(n==1){
			cout<<10000<<endl ;
			continue; 
		}
		
	vector<int>a(n,0) ;
	
	f(j,0,n){
		a[j] = s[j] - 'A' ;
	}
	

	
	vector<vector<int>>dp(n,vector<int>(5,0)) ;
	vector<int>mx(n,0) ;
	vector<int>val(n+1,0) ;
	
	int ans = -1e9 ;
	
	fd(j,n-1,0){
		if(j<n-1)
		mx[j] = max(a[j],mx[j+1]) ; 
		else
		mx[j] = a[j] ;
		
		if(a[j] < mx[j] ){
			val[j] = val[j+1] - power(10,a[j]) ;
		}
		else{
			val[j] = val[j+1] + power(10,a[j]) ;
		}
	//	cout<<mx[j]<<endl ;
	}
	
	f(j,0,5){
		
		if( a[0] >= j  ){
			dp[0][j] = power(10,a[0]) ; 
		}
		else{
			dp[0][j] = -power(10,a[0]) ;
		}
	}
	
	f(j,1,n){
		
		f(i,0,5){
			int k = max(i,a[j]) ;
			if(a[j] >= i )
			dp[j][i] = dp[j-1][k] + power(10,a[j]) ;
			else
			dp[j][i] = dp[j-1][k] - power(10,a[j]) ;
		//	cout<<dp[j][i]<<" " ;
		}
		
	//	cout<<endl ;
	}
	
	fd(j,n-1,0){
		
		if(j==n-1){
			
			f(i,0,5){
				int k = power(10,i) ;
				ans = max(ans,dp[j-1][i] + k ) ;
			}
			
			
		}
		else{
			
			int allowed = mx[j+1] ;
			
			f(i,allowed,5){
				int k = power(10,i) ;
			//	cout<<k<<endl ;
				int mm = 0 ;
				
				if(j>0){
					mm = dp[j-1][i] ;
				}
				ans = max(ans,mm + k+ val[j+1] ) ;
			}
			
			
		}
		
	}
	
	cout<<ans<<endl ;
	
}
	
	

return 0;
 
}


Comments

Submit
1 Comments
  • 19/6/2023 5:43 - Asia/Calcutta

// Ranom digits are denoted by uppercase Latin letters from A to E. Moreover, the value of the letter
//  A is 1
// , B is 10
// , C is 100
// , D is 1000
// , E is 10000


public class C_Ranom_Numbers{

    public static boolean isPostitve(String str,int idx) {

        for(int i=idx+1;i<str.length();i++){

            if(str.charAt(idx)<str.charAt(i)){
                return false;
            }

        }
        return true;
        
    }
    public static void main(String[] args) {

        String str="EB";
        // String str="EAAABDCA";

        int sum=0;

        for(int i=0;i<str.length();i++){
            int val;

            char curr=str.charAt(i);

            boolean sign=isPostitve(str, i);

            if(curr=='A'){
                val=1;
            }
            else if(curr=='B'){
                val=10;
            } else if(curr=='C'){
                val=100;
            } else if(curr=='D'){
                val=1000;
            } else if(curr=='E'){
                val=10000;
            } else {
                val=0;
            }

            if(sign){
                sum=sum+val;
            } else {
                sum=sum-val;
            }

        }

        System.out.println(sum);
        
    }
}


More Questions

233A - Perfect Permutation
1360A - Minimal Square
467A - George and Accommodation
893C - Rumor
227B - Effective Approach
1534B - Histogram Ugliness
1611B - Team Composition Programmers and Mathematicians
110A - Nearly Lucky Number
1220B - Multiplication Table
1644A - Doors and Keys
1644B - Anti-Fibonacci Permutation
1610A - Anti Light's Cell Guessing
349B - Color the Fence
144A - Arrival of the General
1106A - Lunar New Year and Cross Counting
58A - Chat room
230A - Dragons
200B - Drinks
13A - Numbers
129A - Cookies
1367B - Even Array
136A - Presents
1450A - Avoid Trygub
327A - Flipping Game
411A - Password Check
1520C - Not Adjacent Matrix
1538B - Friends and Candies
580A - Kefa and First Steps
1038B - Non-Coprime Partition
43A - Football